package com.victualler.business.inventory.controller.impl;

import com.victualler.business.inventory.controller.InventoryOrderController;
import com.victualler.business.inventory.dto.InventoryOrderInfoDto;
import com.victualler.business.inventory.dto.print.InventoryPrintDto;
import com.victualler.business.inventory.dto.request.InventoryRequestDto;
import com.victualler.business.inventory.entity.InventoryOrderDao;
import com.victualler.business.inventory.repository.InventoryOrderQueryRepository;
import com.victualler.business.inventory.repository.InventoryOrderRepository;
import com.victualler.business.inventory.service.InventoryOrderService;
import com.victualler.core.base.controller.impl.BaseRestControllerImpl;
import com.victualler.core.common.dto.JasperReportVO;
import com.victualler.core.common.dto.PageDto;
import com.victualler.core.common.enums.JasperTypeEnum;
import com.victualler.core.common.utils.ExceptionWapperUtil;
import com.victualler.core.common.utils.ReportUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Guoshichang
 * @discription:    
 */
@RestController
@RequestMapping("/business/inventory/order")
public class InventoryOrderControllerImpl extends BaseRestControllerImpl<InventoryOrderDao,InventoryOrderRepository,InventoryOrderQueryRepository,InventoryOrderService> implements InventoryOrderController {

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param inventoryRequestDto 请求对象
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    @RequestMapping("/getByRequestDto")
    public PageDto<InventoryOrderDao> getByRequestDto(@RequestBody InventoryRequestDto inventoryRequestDto, @RequestParam("pageNo") int pageNo, @RequestParam("limit") int limit) {
        return this.baseService.getByRequestDto(inventoryRequestDto,pageNo,limit);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    @RequestMapping("/findInfo/{id}")
    public InventoryOrderInfoDto findInfo(@PathVariable("id") Long id) {
        return this.baseService.findInfo(id);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param inventoryOrderInfoDto 预订单实体大对象
     * @Function  保存预订单实体大对象
     */
    @RequestMapping("/saveInfo")
    public void saveInfo(@RequestBody InventoryOrderInfoDto inventoryOrderInfoDto) {
        this.baseService.saveInfo(inventoryOrderInfoDto);
    }

    /**
     * @author Guoshichang
     * @since 18:01 on 2018/10/9.
     * @param inventoryOrderDao 审查大对象
     * @return
     * @Function
     */
    @RequestMapping("/check")
    public void check(@RequestBody InventoryOrderDao inventoryOrderDao) {
        this.baseService.check(inventoryOrderDao);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    @RequestMapping("/deleteInfo/{id}")
    public void deleteInfo(@PathVariable("id") Long id) {
        this.baseService.deleteInfo(id);
    }



    /**
     * @author Guoshichang
     * @since 19:54 on 2018/7/23.
     * @param response 存放文件的 输出流
     * @param orderNo 订单号
     * @Function 将文件从影像系统下载下来并且打印返回到response的流中
     */
    @RequestMapping("/exportFile/{orderNo}")
    public void exportFile(@PathVariable("orderNo") String orderNo, HttpServletResponse response) {
        //输出
        try {
            //从service中获取对应的对象
            List<InventoryPrintDto>  modelList = this.baseService.getExportModelList(orderNo);
            //输出到文件流
            response.setContentType(JasperTypeEnum.PDF.getContentType());
            ReportUtil.reportData(Arrays.asList(new JasperReportVO("/jasper/InventoryPrint.jasper",modelList)),null,JasperTypeEnum.PDF,response.getOutputStream());
        } catch (IOException e) {
            throw ExceptionWapperUtil.wapper(e);
        }
    }
}
